package com.google.code.avajhelpdesk.pages.users;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.google.code.avajhelpdesk.entities.User;
import com.google.code.avajhelpdesk.model.GenericLoadableDetachableModel;
import com.google.code.avajhelpdesk.services.UserService;

public class SortableUserDataProvider extends SortableDataProvider<User> {

	private static final long serialVersionUID = 1L;
	
	@SpringBean
	private UserService userService;

	public SortableUserDataProvider() {
		InjectorHolder.getInjector().inject(this);
		setSort("username", true);
	}
	
	public Iterator<? extends User> iterator(int first, int count) {
		SortParam sp = getSort();
		return userService.findUsers(first, count, sp.getProperty(), sp.isAscending()).iterator();
	}

	public IModel<User> model(User object) {
		return new GenericLoadableDetachableModel<User>(object);
	}

	public int size() {
		return userService.userCount();
	}
}